package org.locker.service.impl;

import org.locker.common.core.utils.MapstructUtils;
import org.locker.common.core.utils.StringUtils;
import org.locker.common.mybatis.core.page.TableDataInfo;
import org.locker.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.locker.domain.bo.ManagerFeesBo;
import org.locker.domain.vo.ManagerFeesVo;
import org.locker.domain.ManagerFees;
import org.locker.mapper.ManagerFeesMapper;
import org.locker.service.IManagerFeesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 代理商费用记录(场地)Service业务层处理
 *
 * @author winnie
 * @date 2023-10-11
 */
@RequiredArgsConstructor
@Service
public class ManagerFeesServiceImpl  extends ServiceImpl<ManagerFeesMapper,ManagerFees>  implements IManagerFeesService {

    private final ManagerFeesMapper baseMapper;

    /**
     * 查询代理商费用记录(场地)
     */
    @Override
    public ManagerFeesVo queryById(Long managerFeesId){
        return baseMapper.selectVoById(managerFeesId);
    }

    /**
     * 查询代理商费用记录(场地)列表
     */
    @Override
    public TableDataInfo<ManagerFeesVo> queryPageList(ManagerFeesBo bo, PageQuery pageQuery) {
        Page<ManagerFeesVo> result = baseMapper.listByPage(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询代理商费用记录(场地)列表
     */
    @Override
    public List<ManagerFeesVo> queryList(ManagerFeesBo bo) {
        LambdaQueryWrapper<ManagerFees> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ManagerFees> buildQueryWrapper(ManagerFeesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ManagerFees> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSysUserId() != null, ManagerFees::getSysUserId, bo.getSysUserId());
        lqw.eq(bo.getPositionId() != null, ManagerFees::getPositionId, bo.getPositionId());
        lqw.eq(bo.getAmount() != null, ManagerFees::getAmount, bo.getAmount());
        lqw.eq(bo.getFreezeAmount() != null, ManagerFees::getFreezeAmount, bo.getFreezeAmount());
        lqw.eq(bo.getResidueAmount() != null, ManagerFees::getResidueAmount, bo.getResidueAmount());
        return lqw;
    }

    /**
     * 新增代理商费用记录(场地)
     */
    @Override
    public Boolean insertByBo(ManagerFeesBo bo) {
        ManagerFees add = MapstructUtils.convert(bo, ManagerFees.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setManagerFeesId(add.getManagerFeesId());
        }
        return flag;
    }

    /**
     * 修改代理商费用记录(场地)
     */
    @Override
    public Boolean updateByBo(ManagerFeesBo bo) {
        ManagerFees update = MapstructUtils.convert(bo, ManagerFees.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ManagerFees entity){
        //做一些数据校验,如唯一约束
    }

    /**
     * 批量删除代理商费用记录(场地)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
